package com.delu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.delu.data.Result;
import com.delu.em.ResultEnum;
import com.delu.entity.*;
import com.delu.mapper.CouponMapper;
import com.delu.mapper.CouponTypeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2021-12-19
 */
@RestController
@Api(value = "优惠券服务接口")
@RequestMapping("couponController")
@RefreshScope //开启自动刷新配置
public class CouponController {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private CouponTypeMapper couponTypeMapper;

    @PostMapping
    @ApiOperation("优惠券增加方法")
    //paramType:body 表示@RequestBody接受的实体类 path:表示路径/{} 传参  query @RequestParam接收参数
    @ApiImplicitParam(name = "coupon",value = "优惠券实体类",required = true,paramType = "body")
    public Result insertCoupon(@RequestBody Coupon coupon) {
        try {
            Coupon coupon1 = JSONObject.parseObject(JSON.toJSONString(coupon), Coupon.class);
            coupon1.setCouponStatus(1);
            couponMapper.insert(coupon1);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PutMapping//@RequestParam java中的数据类型如string 加上此注解表名参数为必填项
    //@RequestBody  就收实体类要求前台必须用json的格式传参
    @ApiOperation(value = "优惠券实体类修改")
    @ApiImplicitParam(name = "coupon",value = "优惠券实体类",required = true,paramType = "path")
    public Result updateCouponById(@RequestBody Coupon coupon) {
        try {
            Coupon coupon1 = JSONObject.parseObject(JSON.toJSONString(coupon), Coupon.class);
            couponMapper.updateById(coupon1);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @GetMapping
    @ApiOperation(value = "根据优惠券id查询")
    @ApiImplicitParam(name = "couponId",value = "优惠券id",required = true,paramType = "query")
    public Result selectCouponById(@RequestParam("couponId") Integer couponId) {
        try {
            System.out.println(couponId);
            CouponVO vo = new CouponVO();
            Coupon coupon = couponMapper.selectById(couponId);
            System.out.println(coupon);
            vo.setCoupon(coupon);
            if (coupon.getCouponStatus() != 0){
                CouponType couponType = couponTypeMapper.selectById(coupon.getCouponType());
                vo.setCouponType(couponType);
                return Result.response(ResultEnum.SUCCESS).setData(vo);
            }
            return Result.response(ResultEnum.NOT_COUPON);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @DeleteMapping("/{couponId}")
    @ApiOperation(value = "根据优惠券id删除")
    @ApiImplicitParam(name = "couponId",value = "优惠券id",required = true,paramType = "path")
    public Result deleteCouponById(@PathVariable(value = "couponId",required = true) Integer couponId) {
        try {
            Coupon coupon = couponMapper.selectById(couponId);
            coupon.setCouponStatus(0);
            couponMapper.updateById(coupon);
            return Result.response(ResultEnum.SUCCESS).setData("ok");
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }

    @PostMapping("selectAll")
    @ApiOperation(value = "查询所有优惠券")
    public Result selectCouponList(){
        try {
            List<Coupon> coupons = couponMapper.selectList(null);
            return Result.response(ResultEnum.SUCCESS).setData(coupons);
        }catch (Exception e){
            e.printStackTrace();
            return Result.response(ResultEnum.ERROR).setData(e);
        }
    }
}
